package com.quanshu.exchange.ui.gui.message.friends;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baselibrary.util.glide.GlideUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.chat.ChatConstant;
import com.quanshu.exchange.support.db.UserDao;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.exchange.ui.gui.message.chat.ChatActivity;
import com.sevenheaven.iosswitch.ShSwitchView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 聊天设置
 * Created by xiniu003 on 2017/12/22.
 */

public class PrivateChatDetailActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.user_avatar)
    ImageView userAvatar;
    @BindView(R.id.user_name_text)
    TextView userNameText;
    @BindView(R.id.user_avatar_rel)
    RelativeLayout userAvatarRel;
    @BindView(R.id.chat_setting_shield)
    ShSwitchView chatSettingShield;
    @BindView(R.id.clear_chat_record)
    RelativeLayout clearChatRecord;
    @BindView(R.id.enter)
    Button enter;
    @BindView(R.id.remark_name)
    TextView remarkName;
    @BindView(R.id.set_remark_layout)
    RelativeLayout setRemarkLayout;
    private UserEntity userEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_setting);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initListener();
    }

    private void initIntent() {
        userEntity = (UserEntity) getIntent().getSerializableExtra("UserEntity");
    }

    private void initView() {
        if (userEntity != null) {
            GlideUtils.setRoundImageView(this, userEntity.getAvatar(), R.drawable.default_avatar, userAvatar);
            userNameText.setText(userEntity.getNickname());
            remarkName.setText(AppUtils.getUserName(userEntity));
        }
    }

    private void initListener() {
        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(userAvatarRel, aVoid -> goDetailActivity());
        chatSettingShield.setOnSwitchStateChangeListener(isOn -> doSwitch(isOn));
        RxViewHelper.getInstance().clicks(clearChatRecord, aVoid -> doClearChatRecord());
        RxViewHelper.getInstance().clicks(enter, aVoid -> goChatActivity());
        RxViewHelper.getInstance().clicks(setRemarkLayout, aVoid -> goSetRemarkActivity());
    }

    private void goSetRemarkActivity() {

        Intent intent = new Intent(PrivateChatDetailActivity.this, SetRemarkActivity.class);
        intent.putExtra("UserEntity", userEntity);
        startActivityForResult(intent, 0x01);
    }

    private void goDetailActivity() {

        Intent intent = new Intent(PrivateChatDetailActivity.this, UserDetailActivity.class);
        userEntity.setIs_friend(1);
        intent.putExtra("UserEntity", userEntity);
        startActivityForResult(intent, 0x02);
    }

    private void goChatActivity() {
        Intent intent = new Intent(this, ChatActivity.class);
        intent.putExtra("CHAT_TYPE", ChatConstant.CHAT_TYPE_SINGLE);
        intent.putExtra("conversationID", userEntity.getMemberid());
        startActivity(intent);
    }

    private void doClearChatRecord() {
        //删除当前会话的某条聊天记录
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(userEntity.getMemberid());
        conversation.clearAllMessages();
        ToastHelper.toast("清空聊天纪录成功！");
    }

    private void doSwitch(boolean isOn) {
        userEntity.setShieldingMsg(isOn);
        UserDao.getInstance().save(userEntity);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0x01) {
                userEntity = MyApplication.getInstance().getCurrentUser();
                remarkName.setText(AppUtils.getUserName(userEntity));
            } else if (requestCode == 0x02) {
                setResult(RESULT_OK);
                finish();
            }
        }
    }
}
